package com.ltv.xiangqiplugin.utils;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

import com.ltv.xiangqiplugin.models.Game;

public class XiangPluginConfiguration {
    // plugin_version
    private static final byte[] ENCRYPTED_PLUGIN_VERSION_KEY = new byte[] {3,11,-66,101,53,-74,-53,-96,33,81,-76,108,52,-91,40,-27,-66,-47,-77,-47,2,46,54,-11,50,15,9,51,-79,28,-84,18,62,-82,-106,23,72,54,1,30,19,-18,-62,-125,-61,-40,73,35};
    
    // gae_application
    private static final byte[] ENCRYPTED_GAE_APPLICATION_KEY = new byte[] {48,-124,-106,61,-76,-21,-91,-111,46,-6,-104,-51,-109,59,92,-28,-103,102,96,-61,95,-46,-71,72,-28,61,-1,-17,-40,96,-24,-26,62,-82,-106,23,72,54,1,30,19,-18,-62,-125,-61,-40,73,35};
    
    // copyright
    private static final byte[] ENCRYPTED_COPYRIGHT_KEY = new byte[] {73,40,102,-81,121,41,-90,-78,-22,-117,72,10,101,-45,33,-43,-7,34,-28,70,86,54,28,44,10,-54,-119,-58,86,-40,-125,-121,62,-82,-106,23,72,54,1,30,19,-18,-62,-125,-61,-40,73,35};
    
    // /conf/plugin.conf
    private static final byte[] ENCRYPTED_CONFIG_FILE_NAME_KEY = new byte[] {-105,-73,-73,-4,122,-23,115,-10,44,-105,-37,116,-108,107,-69,-37,126,-22,-87,-118,-100,-89,2,5,-97,-49,51,-98,77,80,62,-121};
    
    // locpx
    private static final byte[] ENCRYPTED_LOCPX_KEY = new byte[] {-21,110,39,-30,70,38,-32,7,23,-54,83,70,125,-1,-69,-92,109,-107,-31,-56,-128,26,-8,-58,-101,-33,-45,123,-74,-119,-52,81,62,-82,-106,23,72,54,1,30,19,-18,-62,-125,-61,-40,73,35};
    
    // thongtcm
    private static final byte[] ENCRYPTED_THONGTCM_KEY = new byte[] {-107,-97,69,-75,-11,105,96,-45,123,-39,-70,-114,12,124,-55,-26,-124,-40,86,76,-91,115,80,10,-20,-32,-63,-70,66,51,-47,47,62,-82,-106,23,72,54,1,30,19,-18,-62,-125,-61,-40,73,35};

    // phucpd
    private static final byte[] ENCRYPTED_PHUCPD_KEY = new byte[] {-3,-13,43,-45,116,85,-31,13,-35,9,125,-120,122,104,-52,-92,-33,-32,-111,92,73,59,65,127,86,116,94,-124,45,-12,93,119,62,-82,-106,23,72,54,1,30,19,-18,-62,-125,-61,-40,73,35};

    // object_classid
    private static final String OBJECT_CLASSID_KEY = "object_classid";
    
    // object_codebase
    private static final String OBJECT_CODEBASE_KEY = "object_codebase";
    
    // embed_pluginspage
    private static final String EMBED_PLUGINSPAGE_KEY = "embed_pluginspage";
    
    private static final String CONFIG_ENCODING = "UTF-8";
    
    private Properties props;
    
    private static XiangPluginConfiguration instance;
    
    private XiangPluginConfiguration() {
        props = new Properties();
        try {
			loadConfiguration();
		} catch (Exception e) {
			e.printStackTrace();
		}
    }

    public static XiangPluginConfiguration getInstance() {
        if (instance == null) {
            instance = new XiangPluginConfiguration();
        }
        return instance;
    }
    
    public void loadConfiguration() throws Exception {
        InputStream is = null;
        InputStreamReader isr = null;
        try {
            is = getClass().getResourceAsStream(new String(AES.decrypt(ENCRYPTED_CONFIG_FILE_NAME_KEY, Game.legalChars.getBytes(CONFIG_ENCODING)), CONFIG_ENCODING));
            isr = new InputStreamReader(is, CONFIG_ENCODING);
            props.load(isr);
        } finally {
            if (is != null) {
                is.close();
            }
            if (isr != null) {
                isr.close();
            }
        }
    }
    
    private byte[] stringToByteArray(String str) {
    	String strArray[] = str.split(",");
    	
    	byte[] ret = new byte[strArray.length];
		int i = 0;
		for (String s: strArray) {
			ret[i] = (byte) Integer.parseInt(s);
			i++;
		}
		
		return ret;
    }
    
    public String getPluginVersion() {
    	try {
    		String key = new String(AES.decrypt(ENCRYPTED_PLUGIN_VERSION_KEY, Game.legalChars.getBytes(CONFIG_ENCODING)), CONFIG_ENCODING);
			String encryptedValue = props.getProperty(key);
			return new String(AES.decrypt(stringToByteArray(encryptedValue), Game.legalChars.getBytes(CONFIG_ENCODING)), CONFIG_ENCODING);
		} catch (Exception e) {
			return "";
		}
    }
    
    public String getGaeApplication() {
    	try {
    		String key = new String(AES.decrypt(ENCRYPTED_GAE_APPLICATION_KEY, Game.legalChars.getBytes(CONFIG_ENCODING)), CONFIG_ENCODING);
    		String encryptedValue = props.getProperty(key);
			return new String(AES.decrypt(stringToByteArray(encryptedValue), Game.legalChars.getBytes(CONFIG_ENCODING)), CONFIG_ENCODING);
		} catch (Exception e) {
			return "";
		}
    }
    
    public String getCopyright() {
    	try {
    		String key = new String(AES.decrypt(ENCRYPTED_COPYRIGHT_KEY, Game.legalChars.getBytes(CONFIG_ENCODING)), CONFIG_ENCODING);
    		String encryptedValue = props.getProperty(key);
			return new String(AES.decrypt(stringToByteArray(encryptedValue), Game.legalChars.getBytes(CONFIG_ENCODING)), CONFIG_ENCODING);
		} catch (Exception e) {
			return "";
		}
    }
    
    public String getLocPX() {
    	try {
    		String key = new String(AES.decrypt(ENCRYPTED_LOCPX_KEY, Game.legalChars.getBytes(CONFIG_ENCODING)), CONFIG_ENCODING);
			String encryptedValue = props.getProperty(key);
			return new String(AES.decrypt(stringToByteArray(encryptedValue), Game.legalChars.getBytes(CONFIG_ENCODING)), CONFIG_ENCODING);
		} catch (Exception e) {
			return "";
		}
    }
    
    public String getThongTCM() {
    	try {
    		String key = new String(AES.decrypt(ENCRYPTED_THONGTCM_KEY, Game.legalChars.getBytes(CONFIG_ENCODING)), CONFIG_ENCODING);
			String encryptedValue = props.getProperty(key);
			return new String(AES.decrypt(stringToByteArray(encryptedValue), Game.legalChars.getBytes(CONFIG_ENCODING)), CONFIG_ENCODING);
		} catch (Exception e) {
			return "";
		}
    }
    
    public String getPhucPD() {
    	try {
    		String key = new String(AES.decrypt(ENCRYPTED_PHUCPD_KEY, Game.legalChars.getBytes(CONFIG_ENCODING)), CONFIG_ENCODING);
			String encryptedValue = props.getProperty(key);
			return new String(AES.decrypt(stringToByteArray(encryptedValue), Game.legalChars.getBytes(CONFIG_ENCODING)), CONFIG_ENCODING);
		} catch (Exception e) {
			return "";
		}
    }
    
    public String getObjectClassID() {
    	return props.getProperty(OBJECT_CLASSID_KEY);
    }
    
    public String getObjectCodeBase() {
    	return props.getProperty(OBJECT_CODEBASE_KEY);
    }
    
    public String getEmbedPluginsPage() {
    	return props.getProperty(EMBED_PLUGINSPAGE_KEY);
    }
    
    public static void main(String[] args) {
		System.out.println(XiangPluginConfiguration.getInstance().getObjectClassID());
		System.out.println(XiangPluginConfiguration.getInstance().getObjectCodeBase());
		System.out.println(XiangPluginConfiguration.getInstance().getEmbedPluginsPage());
	}
}
